#include "StdAfx.h"
#include "monster.h"


monster::monster(int z, int t)
{
	swingDelay = false;
	xVel = 0.0;
	yVel = 0.0;
	x = z;
	y = t;
	box.x = x;
	box.y = y;
	box.w = 50;
	box.h = 50;
	frame = 1;
	framesEach =50;
	frameCal = 0;
	strength = 30;
	dextarity = 15;
	maxHealth = strength * 1.6f;
	armor = strength * .55f;
	damage = strength * .5f;
	dodgeChance = dextarity * .30f;
	critChance = dextarity * .40f;
	health = maxHealth;
	alive = true;
	second = 0;
	HBar.x = x;
	HBar.y = y - 15.f;
	HBar.h = 10;
	HBar.w = health;
	
};
monster::~monster()
{
};
void monster::runInCircles(int deltaTicks)
{
	if(alive)
	{
		if(frame > 4)
			frame = 1;
	
		switch(frame)
		{
		case 1:
			xVel = 100.0;
			yVel = 0;
			break;
		case 2:
			xVel = 0;
			yVel = 100.0;
			break;
		case 3:
			xVel = -100.0;
			yVel = 0;
			break;
		case 4:
			xVel = 0;
			yVel = -100.0;
			break;
		}
		x += xVel * deltaTicks / 1000.f;
		y += yVel * deltaTicks / 1000.f;
		box.x = x;
		box.y = y;
		
	}
};
void monster::follow(int deltaTicks, int xa, int ya)
{
	if(alive)
	{
		int xDif = x - xa;
		int yDif = y - ya;
		if(xDif > 10)
		{
			xVel = -100.0;
		}
		if(xDif < -10)
		{
			xVel = 100.0;
		}
		if(yDif < -10)
		{
			yVel = 100.0;
		}
		if(yDif > 10)
		{
			yVel = -100.0;
		}
		if(xDif >= 150 || xDif <= -150 || yDif >= 150 || yDif <= -150)
		{
			xVel = 0;
			yVel = 0;
		}
		x += xVel * deltaTicks / 1000.f;
		y += yVel * deltaTicks / 1000.f;
		box.x = x;
		box.y = y;

	}
	
}
int monster::attack(int armor, int dodgeChance)
{
	if(!alive)
		return 0;
	else
	{
		int trueDamage;

		trueDamage = damage - (armor * .45);

		if(trueDamage <= 0)
			trueDamage = 0;

		int dodge = rand()%100+1;
		if(dodge >= 0 && dodge <= dodgeChance)
			trueDamage = 0;

		int crit = rand()%100+1;
		if(crit >= 0 && crit <= critChance)
			trueDamage = trueDamage * 2;

		return trueDamage;
	}
};
void monster::updateBar()
{
	HBar.x = box.x;
	HBar.y = y - 15.f;
	int maxPixels = 50;
	float pixels = maxPixels / maxHealth;
	HBar.w = health * pixels;
};
void monster::respawn()
{
	if(second == 5)
	{
		second = 0;
		alive = true;
		strength = 30;
		dextarity = 25;
		maxHealth = strength * 1.6f;
		armor = strength * .55f;
		damage = strength * .5f;
		dodgeChance = dextarity * .40f;
		critChance = dextarity * .40f;
		health = maxHealth;
		int z = rand()%200 + 100;
		int t = rand()%200 + 100;
		x = z;
		y = t;
		box.x = x;
		box.y = y;
	}
	else second++;
};
